/**
 * Music Library Page Tests
 * 
 * Tests for the music library page functionality
 * Validates: Requirements 3.4, 3.5
 */

const assert = require('assert')

// Simple test runner
console.log('Running Music Library Page Tests...\n')

let passed = 0
let failed = 0
const tests = []

function test(name, fn) {
  tests.push({ name, fn })
}

function runTests() {
  tests.forEach(({ name, fn }) => {
    try {
      fn()
      console.log(`✓ ${name}`)
      passed++
    } catch (error) {
      console.log(`✗ ${name}`)
      console.log(`  ${error.message}`)
      failed++
    }
  })
}

// Define tests
test('should format duration correctly', () => {
  const formatDuration = (seconds) => {
    if (!seconds) return '00:00'
    const mins = Math.floor(seconds / 60)
    const secs = seconds % 60
    return `${mins.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`
  }
  
  assert.strictEqual(formatDuration(0), '00:00')
  assert.strictEqual(formatDuration(30), '00:30')
  assert.strictEqual(formatDuration(60), '01:00')
  assert.strictEqual(formatDuration(125), '02:05')
  assert.strictEqual(formatDuration(3661), '61:01')
})

test('should format date correctly', () => {
  const formatDate = (dateString) => {
    if (!dateString) return ''
    const date = new Date(dateString)
    const year = date.getFullYear()
    const month = (date.getMonth() + 1).toString().padStart(2, '0')
    const day = date.getDate().toString().padStart(2, '0')
    return `${year}-${month}-${day}`
  }
  
  // Test with a date string that won't have timezone issues
  const result1 = formatDate('2024-01-15T10:30:00Z')
  assert.ok(result1.startsWith('2024-01'))
  
  const result2 = formatDate('')
  assert.strictEqual(result2, '')
})

test('should extract location info from metadata', () => {
  const getLocationInfo = (metadata) => {
    if (!metadata) return '未知地点'
    if (metadata.poi_name) return metadata.poi_name
    if (metadata.location) {
      return `${metadata.location.latitude.toFixed(4)}, ${metadata.location.longitude.toFixed(4)}`
    }
    return '未知地点'
  }
  
  assert.strictEqual(getLocationInfo(null), '未知地点')
  assert.strictEqual(getLocationInfo({}), '未知地点')
  assert.strictEqual(getLocationInfo({ poi_name: '天安门' }), '天安门')
  assert.strictEqual(
    getLocationInfo({ location: { latitude: 39.9042, longitude: 116.4074 } }), 
    '39.9042, 116.4074'
  )
})

test('should display music metadata correctly', () => {
  const music = {
    id: 'music-1',
    file_url: 'https://example.com/music.mp3',
    duration: 180,
    created_at: '2024-01-15T10:30:00Z',
    metadata: {
      poi_name: '故宫',
      weather: {
        condition: '晴',
        temperature: 20
      },
      mood: '轻松'
    }
  }
  
  assert.ok(music.metadata.poi_name)
  assert.ok(music.metadata.weather)
  assert.ok(music.metadata.mood)
  assert.strictEqual(music.metadata.poi_name, '故宫')
  assert.strictEqual(music.metadata.weather.condition, '晴')
  assert.strictEqual(music.metadata.weather.temperature, 20)
  assert.strictEqual(music.metadata.mood, '轻松')
})

test('should handle play/pause toggle', () => {
  let currentPlayingId = null
  const musicId = 'music-1'
  
  // First click - should play
  if (currentPlayingId === musicId) {
    currentPlayingId = null
  } else {
    currentPlayingId = musicId
  }
  assert.strictEqual(currentPlayingId, musicId)
  
  // Second click - should pause
  if (currentPlayingId === musicId) {
    currentPlayingId = null
  } else {
    currentPlayingId = musicId
  }
  assert.strictEqual(currentPlayingId, null)
})

test('should filter music list on delete', () => {
  const musicList = [
    { id: 'music-1', name: 'Music 1' },
    { id: 'music-2', name: 'Music 2' },
    { id: 'music-3', name: 'Music 3' }
  ]
  
  const musicIdToDelete = 'music-2'
  const filteredList = musicList.filter(music => music.id !== musicIdToDelete)
  
  assert.strictEqual(filteredList.length, 2)
  assert.strictEqual(filteredList[0].id, 'music-1')
  assert.strictEqual(filteredList[1].id, 'music-3')
  assert.ok(!filteredList.find(m => m.id === musicIdToDelete))
})

test('should generate share info correctly', () => {
  const musicId = 'music-123'
  const musicName = '故宫音乐'
  
  const shareInfo = {
    title: `分享音乐: ${musicName}`,
    path: `/pages/music-library/music-library?musicId=${musicId}`,
    imageUrl: ''
  }
  
  assert.strictEqual(shareInfo.title, '分享音乐: 故宫音乐')
  assert.strictEqual(shareInfo.path, '/pages/music-library/music-library?musicId=music-123')
})

// Run all tests
runTests()

console.log(`\nTests: ${passed} passed, ${failed} failed, ${passed + failed} total`)

if (failed > 0) {
  process.exit(1)
}
